package com.ld.admin.model.system.systeminfo.server;

import java.lang.management.ManagementFactory;
import java.util.Date;

import com.ld.shieldsb.common.core.util.ArithUtil;
import com.ld.shieldsb.common.core.util.DateUtil;

import lombok.Data;

/**
 * JVM相关信息
 * 
 */
@Data
public class JvmInfo {

    /**
     * 项目线程数
     */
    private int threadCount;

    /**
     * 当前JVM占用的内存总数(M)
     */
    private double total;

    /**
     * JVM最大可用内存总数(M)
     */
    private double max;

    /**
     * JVM空闲内存(M)
     */
    private double free;

    /**
     * JDK版本
     */
    private String version;

    /**
     * JDK路径
     */
    private String home;

    public double getTotal() {
        return ArithUtil.div(total, (1024 * 1024), 2);
    }

    public double getMax() {
        return ArithUtil.div(max, (1024 * 1024), 2);
    }

    public double getFree() {
        return ArithUtil.div(free, (1024 * 1024), 2);
    }

    public double getUsed() {
        return ArithUtil.div(total - free, (1024 * 1024), 2);
    }

    public double getUsage() {
        return ArithUtil.mul(ArithUtil.div(total - free, total, 4), 100);
    }

    /**
     * 获取JDK名称
     */
    public String getName() {
        return ManagementFactory.getRuntimeMXBean().getVmName();
    }

    /**
     * JDK启动时间
     */
    public String getStartTime() {
        return DateUtil.getDateStr(getServerStartDate(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * JDK运行时间
     */
    public String getRunTime() {
        return DateUtil.formatTimeBetween(getServerStartDate(), new Date());
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

}
